package com.wkbb.common.handler;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.*;

public class StringArrayTypeHandler extends BaseTypeHandler<String[]> {

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, String[] parameter, JdbcType jdbcType)
			throws SQLException {
		if(parameter==null) {
			ps.setNull(i, Types.VARCHAR);
		}else {
			ps.setString(i, StringUtils.join(parameter, ","));
		}
	}

	@Override
	public String[] getNullableResult(ResultSet rs, String columnName) throws SQLException {
		return getStringArray(rs.getString(columnName));
	}

	@Override
	public String[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		return getStringArray(rs.getString(columnIndex));
	}

	@Override
	public String[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
		return getStringArray(cs.getString(columnIndex));
	}

	private String[] getStringArray(String columnValue) {
		if (columnValue == null) {
			return null;
		}
		return StringUtils.split(columnValue,",");
	}

	/*private static final String TYPE_NAME_VARCHAR = "varchar";
	private static final String TYPE_NAME_INTEGER = "integer";
	private static final String TYPE_NAME_BOOLEAN = "boolean";
	private static final String TYPE_NAME_NUMERIC = "numeric";

	@Override
	public Object[] getNullableResult(ResultSet arg0, String arg1) throws SQLException {
		return getArray(arg0.getArray(arg1));
	}

	@Override
	public Object[] getNullableResult(ResultSet arg0, int arg1) throws SQLException {
		return getArray(arg0.getArray(arg1));
	}

	@Override
	public Object[] getNullableResult(CallableStatement arg0, int arg1) throws SQLException {
		return getArray(arg0.getArray(arg1));
	}

	@Override
	public void setNonNullParameter(PreparedStatement arg0, int arg1, Object[] arg2, JdbcType arg3)
			throws SQLException {
		String typeName = null;
		if (arg2 instanceof Integer[]) {
			typeName = TYPE_NAME_INTEGER;
		} else if (arg2 instanceof String[]) {
			typeName = TYPE_NAME_VARCHAR;
		} else if (arg2 instanceof Boolean[]) {
			typeName = TYPE_NAME_BOOLEAN;
		} else if (arg2 instanceof Double[]) {
			typeName = TYPE_NAME_NUMERIC;
		}

		if (typeName == null) {
			throw new TypeException(
					"ArrayTypeHandler parameter typeName error, your type is " + arg2.getClass().getName());
		}

//		if(arg2==null) {
//			arg0.setNull(arg1, Types.VARCHAR);
//		}else {
//            StringBuffer result = new StringBuffer();
//            for (String value : arg2)
//                   result.append(value).append(",");
//            result.deleteCharAt(result.length()-1);
//            arg0.setString(arg1, result.toString());
//     }
		// 这3行是关键的代码，创建Array，然后ps.setArray(i, array)就可以了
		Connection conn = arg0.getConnection();
		arg0.setArray(arg1, conn.createArrayOf(typeName, arg2));
	}

	private Object[] getArray(Array array) {
		if (array == null) {
			return null;
		}

		try {
			return (Object[]) array.getArray();
		} catch (Exception e) {
		}

		return null;
	}*/
}
